package com.darthsith.runtap.activity.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.darthsith.runtap.R;
import com.darthsith.runtap.activity.MainActivity;

public class MenuFragment extends Fragment implements OnClickListener {

	private View thisView;
	private Button practice;
	private Button multiplayer;
	private Button option;
	private Button exit;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		thisView= inflater.inflate(R.layout.menu_fragment, container, false);
		multiplayer=(Button) thisView.findViewById(R.id.multiplayer);
		option=(Button) thisView.findViewById(R.id.option);
		practice=(Button) thisView.findViewById(R.id.practice);
		exit=(Button) thisView.findViewById(R.id.exit);
		practice.setOnClickListener(this);
		multiplayer.setOnClickListener(this);
		option.setOnClickListener(this);
		exit.setOnClickListener(this);
		thisView.findViewById(R.id.sign_in_button).setOnClickListener(this);
		thisView.findViewById(R.id.sign_out_button).setOnClickListener(this);
		thisView.findViewById(R.id.achievement).setOnClickListener(this);
		thisView.findViewById(R.id.leaderboard).setOnClickListener(this);
		Log.i(getTag(), "Load MenuFragment");
		return thisView;
	}
	
	@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
        	onButtonClickedListener = (OnButtonClickedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnButtonClickedListener");
        }
    }
	
	@Override
	public void onResume() {
		super.onResume();
		((MainActivity)getActivity()).refreshSignInButton();
	}
	
	@Override
	public void onClick(View v) {
		if (v.getId()==practice.getId()) {
			onButtonClickedListener.onButtonClicked(practice.getId());
		}
		if (v.getId()==multiplayer.getId()) {
			onButtonClickedListener.onButtonClicked(multiplayer.getId());
		}
		if (v.getId()==option.getId()) {
			onButtonClickedListener.onButtonClicked(option.getId());
		}
		if (v.getId()==exit.getId()) {
			onButtonClickedListener.onButtonClicked(exit.getId());
		}
		if (v.getId()==R.id.sign_in_button) {
			onButtonClickedListener.onButtonClicked(R.id.sign_in_button);
		}
		if (v.getId()==R.id.sign_out_button) {
			onButtonClickedListener.onButtonClicked(R.id.sign_out_button);
		}
		if (v.getId()==R.id.achievement) {
			onButtonClickedListener.onButtonClicked(R.id.achievement);
		}
		if (v.getId()==R.id.leaderboard) {
			onButtonClickedListener.onButtonClicked(R.id.leaderboard);
		}
	}
	
	public interface OnButtonClickedListener {
        public void onButtonClicked(int id); 
	}

	private OnButtonClickedListener onButtonClickedListener = null;

	public OnButtonClickedListener getOnButtonClickedListener() {
		return onButtonClickedListener;
	}

	public void setOnButtonClickedListener(OnButtonClickedListener onButtonClickedListener) {
		this.onButtonClickedListener = onButtonClickedListener;
	}

}
